﻿<MudTreeView ExpandOnClick="true" ServerData="LoadServerData" Items="_treeItems" Width="350px">
    <ItemTemplate>
        <MudTreeViewItem 
            Text="@context.Text"
            Value="@context.Value"
            Items="@context.Children"
            ItemsChanged="@(new Action<IReadOnlyCollection<TreeItemData<string?>>?>(items =>  OnItemsLoaded(context, items)))"
            Icon="@context.Icon"
            CanExpand="@context.Expandable"
            Expanded="@context.Expanded"
            />
    </ItemTemplate>
</MudTreeView>

@code {

    private readonly List<TreeItemData<string?>> _treeItems = [];
    private int _idCounter = 1;
    protected override void OnInitialized()
    {
        _treeItems.Add(new TreeItemData<string?> { Value = "All Mail", Icon = Icons.Material.Filled.Email });
        _treeItems.Add(new TreeItemData<string?>
        {
            Value = "Categories",
            Icon = Icons.Material.Filled.Label,
            Expanded = true,
            Children = new List<TreeItemData<string?>>
            {
                new() { Value = "Social", Icon = Icons.Material.Filled.Group, },
                new() { Value = "Updates", Icon = Icons.Material.Filled.Info,  }
            }
        });
        _treeItems.Add(new TreeItemData<string?> { Value = "Trash", Icon = Icons.Material.Filled.Delete, Expandable = false });
        base.OnInitialized();
    }

    public Task<IReadOnlyCollection<TreeItemData<string?>>?> LoadServerData(string? parentValue)
    {
        return Task.FromResult<IReadOnlyCollection<TreeItemData<string?>>?>(new List<TreeItemData<string?>>
        {
            new() { Value = $"Loaded {_idCounter++}", Icon = Icons.Material.Filled.Web, Expandable  = true }
        });
    }

    private void OnItemsLoaded(TreeItemData<string?> treeItemData, IReadOnlyCollection<TreeItemData<string?>>? children)
    {
        // here we store the server-loaded children in the treeItemData so that they are available in the InitialTreeItems
        // if you don't do this you loose already loaded children on next render update
        treeItemData.Children = children?.ToList();
    }
}